package server;

import java.io.IOException;
import java.io.InputStream;

/**
 * 把请求信息封装为Request对象(根据InputStream输入流封装)
 *
 * @author 陶肖寅
 * @version V1.0
 * @Package server
 * @date 2020-02-24 11:39
 * @Copyright © 2020-2021 城云科技(中国)有限公司
 */
public class Request {
    /**
     * 请求方式
     */
    private String method;
    /**
     * 请求Url
     */
    private String url;
    private InputStream inputStream;//其它属性通过输入流解析

    public Request() {
    }

    public Request(InputStream inputStream) throws IOException {
        //从输入流中获取请求信息
        int count=0;
        while (count == 0 ){
            count = inputStream.available();
        }
        byte[] bytes = new byte[count];
        inputStream.read(bytes);
        String inputStr = new String(bytes);
        //获取第一行请求头信息
       String firstLineStr =  inputStr.split("\\n")[0];
        String[] strings = firstLineStr.split(" ");
        this.method = strings[0];
        this.url = strings[1];
        System.out.println("========>>method:" + method);
        System.out.println("========>>url:" + url);
        System.out.println("========>>请求信息"+new String(bytes));
    }


    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

}
